package com.example.qjy.korean

import android.content.pm.ActivityInfo
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.TextUtils
import com.qjy.adapter.KoreanAdapter
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private var mKoreanLetterList = ArrayList<String>()
    private var mKoreanUnicodeUtil: KoreanUnicodeUtil = KoreanUnicodeUtil()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        if (requestedOrientation != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        }

        var mKoreanLetterArray: Array<String> = resources.getStringArray(R.array.korean_letter)
        for (value in mKoreanLetterArray) {
            mKoreanLetterList.add(value)
        }
        var mkoreanLayoutManager: RecyclerView.LayoutManager = GridLayoutManager(this@MainActivity, 8)

        var mKoreanAdapter: KoreanAdapter = KoreanAdapter(mKoreanLetterList, this@MainActivity)

        mKoreanAdapter.setKoreanLetterOnItemClickListener(mKoreanLetterOnItemClickListener())
        korean_RecyclerView!!.layoutManager = mkoreanLayoutManager
        korean_RecyclerView!!.adapter = mKoreanAdapter
    }

    private var isEnd: Boolean = true

    inner class mKoreanLetterOnItemClickListener : KoreanAdapter.KoreanLetterOnItemClickListener {
        override fun onItemClick(letter: String) {
            var textKorean: String = korean_TextView.text!!.toString()

            if (letter.equals("del")) {
                if (TextUtils.isEmpty(korean_undetermined.text)){
                    if (textKorean.length > 0) {
                        textKorean = textKorean.substring(0, textKorean.length - 1)
                        korean_TextView.text = textKorean
                        isEnd = true
                    }
                }else{
                    korean_undetermined.text = "";
                }

            } else {
                var buf: String = korean_undetermined.text.toString();
//                if (isEnd) {
//                    buf = "";
//                } else {
//                    buf = textKorean.get(textKorean.length - 1).toString();
//                    textKorean = textKorean.substring(0, textKorean.length - 1)
//                }
                var charProcessor = mKoreanUnicodeUtil.charProcessor(letter, buf, null, false)
                var i: Int = 0
                while (i < charProcessor.size) {
                    if (i != charProcessor.size - 1)
                        textKorean = textKorean + charProcessor[i]

                    if (i == charProcessor.size - 1) {
                        if (charProcessor[i].equals("0")) {
                            isEnd = true;
                            korean_undetermined.text = "";
                        } else if (charProcessor[i].equals("1")) {
                            isEnd = false;
                            korean_undetermined.text = textKorean.get(textKorean.length - 1).toString()
                            textKorean = textKorean.substring(0, textKorean.length - 1)
                        }
                    }
                    i++
                }
                korean_TextView.text = textKorean
            }


        }
    }

    inline fun consume(f: () -> Unit): Boolean {
        f()
        return true
    }
}
